<?php

namespace app\widgets;

use app\librarys\Tools;
use app\models\Institution;
use app\models\InstitutionUpdateRecord;
use app\models\Manager;
use yii\base\Widget;

/**
 * Class InstitutionHierarchyAutocompleteWidget
 * @package app\widgets
 */
class InstitutionHierarchyAutocompleteWidget extends Widget
{
    public $institutions;
    public $id;
    public $currentInstitutionCode;
//    private $currentInstitutionText;
    public $defaultText;
    public $tagSourceType;
    public $hasModifiedInstitutionCodeList = null;

    public $isShowCustomPrice = false; // 是否显示特殊定价机构
    public $customPriceInstitutionCodeList = null;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $currentManager = Manager::getCurrentManager();
        $this->institutions = Institution::getAllEnabledListWithHierarchy($currentManager['institution_code']);
        if ($this->defaultText == null) {
            $this->defaultText = '----系统----';
        }

        if ($this->tagSourceType != null) {
            $this->hasModifiedInstitutionCodeList = InstitutionUpdateRecord::getHasModifiedInstitutionCodeList($this->tagSourceType);
        }

        if ($this->isShowCustomPrice == true) {
            $this->customPriceInstitutionCodeList = Institution::getCustomPriceInstitutionList();
        }

//        $this->currentInstitutionText = "";
//        if ($this->currentInstitutionCode != null) {
//            self::_getCurrentInstitutionText($this->institutions);
//        }
    }

//    private function _getCurrentInstitutionText($institutions)
//    {
//        if ($this->currentInstitutionText != "") {
//            return;
//        }
//
//        foreach ($institutions as $institution) {
//            if ($institution['code'] == $this->currentInstitutionCode) {
//                $this->currentInstitutionText = $institution['name'];
//                break;
//            }
//            if (isset($institution['children'])) {
//                self::_getCurrentInstitutionText($institution['children']);
//            }
//
//            if ($this->currentInstitutionText != "") {
//                break;
//            }
//        }
//    }

    public function run()
    {
        $html = "";
        if ($this->institutions == null) {
            parent::run();
        } else {
            $source = json_encode($this->institutions);
            $sourceVar = $this->id . '_institutions';
            $tagSource = json_encode($this->hasModifiedInstitutionCodeList);
            $tagSourceVar = $this->id . '_tagSource';
            $tag1Source = json_encode($this->customPriceInstitutionCodeList);
            $tag1SourceVar = $this->id . '_tag1Source';
            $html = "
            <script type='application/javascript'>
            $(function () {
                var $sourceVar =  $source;
                var $tagSourceVar = $tagSource;
                var $tag1SourceVar = $tag1Source;
                $('#$this->id').treeAutocomplete({
                        defaultText: '$this->defaultText',
                        source: $sourceVar,
                        tagSource:$tagSourceVar,
                        tag1Source:$tag1SourceVar
                }).setValue('$this->currentInstitutionCode');
             });
            </script>
            <div id='$this->id'></div>";
        }

        return $html;
    }
}